home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GLIQ / GLIQ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  7.9 KB  |  410 lines

  1. /*  
  2.  *  CS 453 - Final project : An OpenGL version of the pegboard game IQ
  3.  *  Due : June 5, 1997
  4.  *  Author : Kiri Wagstaff
  5.  *
  6.  *  File : gliq.c
  7.  *  Description : Main board display file.
  8.  *
  9.  *  5/22 : Displays the board selection screen, and uses keyboard
  10.  *  input to manipulate it and select.
  11.  *
  12.  */
  13.  
  14. #include "gliq.h"
  15.  
  16. /* globals */
  17.  
  18. #if 0
  19. //GLuint cone;
  20. #endif
  21. int curstate;
  22. int mouse_state=-1;
  23. int mouse_button=-1;
  24. int pegs=0;
  25. int totalpegs=0;
  26. int lastpicked = 0;
  27.  
  28. /* functions */
  29. void init(void);
  30. void reshape(int width, int height);
  31. void display(void);
  32. void special(int key, int x, int y);
  33. void keyboard(unsigned char key, int x, int y);
  34. void mouse(int button, int state, int x, int y);
  35. void motion(int x, int y);
  36. void idle(void);
  37.  
  38. int main(int argc, char** argv)
  39. {
  40.   glutInit(&argc, argv);
  41.  
  42.   glutInitWindowSize(512, 512);
  43.   glutInitWindowPosition(0, 0);
  44.   glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  45.   glutCreateWindow("GLIQ");
  46.   
  47.   glutReshapeFunc(reshape);
  48.   glutDisplayFunc(display);
  49.   glutKeyboardFunc(keyboard);
  50.   glutSpecialFunc(special);
  51.   glutMouseFunc(mouse);
  52.   glutMotionFunc(motion);
  53.   glutPassiveMotionFunc(passive);
  54.   glutIdleFunc(idle);
  55.   
  56.   init();
  57.   
  58.   glutMainLoop();
  59.   return 0;
  60. }
  61.  
  62.  
  63. void init(void)
  64. {
  65.   int i, j;
  66.  
  67.   /* lighting */
  68.   glEnable(GL_LIGHTING);
  69.   glEnable(GL_LIGHT0);
  70.   glEnable(GL_COLOR_MATERIAL);
  71.  
  72.   glEnable(GL_DEPTH_TEST);
  73.   glDepthFunc(GL_LEQUAL);
  74.  
  75.   /*  glEnable(GL_CULL_FACE);*/
  76.  
  77.   /* put the identity in the trackball transform */
  78.   tbInit(GLUT_RIGHT_BUTTON);
  79.  
  80.   glSelectBuffer(SELECT_BUFFER, select_buffer);
  81.  
  82.   /* make the star cone */
  83.   /*  cone = glGenLists(1);  
  84.   glNewList(cone, GL_COMPILE);
  85.   glPushMatrix();
  86.   for (i=0; i<3; i++)
  87.     {
  88.       glRotatef(45.0, 1.0, 0.0, 0.0);
  89.       glutSolidCone(0.2, 2.0, 8, 8);
  90.     }
  91.   glPopMatrix();
  92.   glEndList();*/
  93.  
  94.   /* Initialize the state */
  95.   for (i=0; i<BOARDSIZE; i++)
  96.     for (j=0; j<BOARDSIZE; j++)
  97.       filled[i][j] = UNUSED;
  98.   curstate = SELBOARD;
  99.   curboard = 0;
  100.   readboards();
  101.   readscores();
  102.  
  103. }
  104.  
  105. void reshape(int width, int height)
  106. {
  107.   GLfloat lightpos[4] = { 1.0, 1.0, 1.0, 1.0 };
  108.   tbReshape(width, height);
  109.  
  110.   glViewport(0, 0, width, height);
  111.   
  112.   glMatrixMode(GL_PROJECTION);
  113.   glLoadIdentity();
  114.   gluPerspective(60.0, (GLfloat)height / (GLfloat)width, 1.0, 128.0);
  115.   glMatrixMode(GL_MODELVIEW);
  116.   glLoadIdentity();
  117.   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
  118.   glTranslatef(0.0, -2.0, -15.0);
  119. }
  120.  
  121. void display(void)
  122. {
  123.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  124.  
  125.   /* draw */
  126.   switch(curstate)
  127.     {
  128.     case SELBOARD:
  129.       selectboard();
  130.       break;
  131.     case PLAY:
  132.       playgame();
  133.       break;
  134.     case HIGHSC:
  135.       highscore();
  136.       break;
  137.     case VIEWSCORES:
  138.       showhighscores();
  139.       break;
  140.     default:
  141.       printf("Unknown state %d, exiting.\n", curstate);
  142.       exit(1);
  143.     }
  144.  
  145.   glutSwapBuffers();
  146. }
  147.  
  148. /* ARGSUSED1 */
  149. void special(int key, int x, int y)
  150. {
  151.   switch (key) { 
  152.     case GLUT_KEY_UP:
  153.       break;
  154.  
  155.     case GLUT_KEY_DOWN:
  156.       break;
  157.  
  158.     case GLUT_KEY_RIGHT:
  159.       break;
  160.  
  161.     case GLUT_KEY_LEFT:
  162.       break;
  163.  }
  164.  
  165.   glutPostRedisplay();
  166. }
  167.  
  168. /* ARGSUSED1 */
  169. void keyboard(unsigned char key, int x, int y)
  170. {
  171.   switch (key) {
  172.   case 'h':
  173.     printf("gliq help\n\n");
  174.     printf("f            -  Filled\n");
  175.     printf("w            -  Wireframe\n");
  176.     printf("s            -  See high scores\n");
  177.     printf("escape or q  -  Quit\n\n");
  178.     break;
  179.  
  180.   case 'f':
  181.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  182.     break;
  183.  
  184.   case 'w':
  185.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  186.     break;
  187.  
  188.   case 's':
  189.     curstate = VIEWSCORES;
  190.     glutIdleFunc(idlescore);
  191.     break;
  192.     
  193.   case 'q':
  194.   case 27:
  195.     exit(0);
  196.     break;
  197.   }
  198.  
  199.   glutPostRedisplay();
  200. }
  201.  
  202. void mouse(int button, int state, int x, int y)
  203. {
  204.   int i, j;
  205.   int mid=0;
  206.   
  207.   mouse_state = state;
  208.   mouse_button = button;
  209.  
  210.   if (state == GLUT_DOWN && button==GLUT_LEFT_BUTTON)
  211.     switch(curstate)
  212.       {
  213.       case SELBOARD:
  214.     switch(picked)
  215.       {
  216.       case NONE:
  217.         break;
  218.       case LEFTARR:
  219.         curboard--;
  220.         totalpegs = 0;
  221.         if (curboard<0)
  222.           curboard = numboards-1;
  223.         /* Set up filled array */
  224.         for (i=0; i<BOARDSIZE; i++)
  225.           for (j=0; j<BOARDSIZE; j++)
  226.         {
  227.           filled[i][j] = boards[curboard][i][j];
  228.           if (filled[i][j] == FULL)
  229.             totalpegs++;
  230.         }
  231.         break;        
  232.       case SELECT:
  233.         totalpegs = 0;
  234.         /* Set up filled array */
  235.         for (i=0; i<BOARDSIZE; i++)
  236.           for (j=0; j<BOARDSIZE; j++)
  237.         {
  238.           filled[i][j] = boards[curboard][i][j];
  239.           if (filled[i][j] == FULL)
  240.             totalpegs++;
  241.         }
  242.         curstate = PLAY;
  243.         playdone = 0;
  244.         pegs = totalpegs;
  245.         glutIdleFunc(NULL);
  246.         break;
  247.       case RIGHTARR:
  248.         curboard++;
  249.         totalpegs = 0;
  250.         if (curboard>=numboards)
  251.           curboard = 0;
  252.         /* Set up filled array */
  253.         for (i=0; i<BOARDSIZE; i++)
  254.           for (j=0; j<BOARDSIZE; j++)
  255.         {
  256.           filled[i][j] = boards[curboard][i][j];
  257.           if (filled[i][j] == FULL)
  258.             totalpegs++;
  259.         }
  260.         break;
  261.       case QUIT:
  262.         exit(0);
  263.       default:
  264.         printf("picked is %d.\n", picked);
  265.       }
  266.     break;
  267.       case PLAY:
  268. #if 0
  269. //    printf("picked is %d\n", picked);
  270. #endif
  271.     if (picked == 0)
  272.       break;
  273.     if (picked == QUIT)
  274.       {
  275.         if (pegs < minscore || (pegs==minscore && totalpegs > minpegs))
  276.           {
  277.         curstate = HIGHSC;
  278.         numentered = 0;
  279.         written = 0;
  280.         glutKeyboardFunc(keyscores);
  281.         glutIdleFunc(idlescore);
  282.         break;
  283.           }
  284.         curstate = SELBOARD;
  285.         glutIdleFunc(idle);
  286.         totalpegs = 0;
  287.         for (i=0; i<BOARDSIZE; i++)
  288.           for (j=0; j<BOARDSIZE; j++)
  289.         {
  290.           filled[i][j] = boards[curboard][i][j];
  291.           if (filled[i][j] == FULL)
  292.             totalpegs++;
  293.         }
  294.         break;
  295.       }
  296.     if (filled[(picked-1)/BOARDSIZE][(picked-1)%BOARDSIZE] == FULL)
  297.       {
  298.         if (canmove(picked))
  299.           filled[(picked-1)/BOARDSIZE][(picked-1)%BOARDSIZE] = CANMOVE;
  300.         else
  301.           {
  302.         filled[(picked-1)/BOARDSIZE][(picked-1)%BOARDSIZE] = CANTMOVE;
  303. #if 0
  304.         Beep(1000, 40);
  305.         PlaySound("SPR_OUCH.WAV", NULL, SND_FILENAME);
  306. #endif
  307.           }
  308.         lastpicked = picked;
  309.       }
  310.     else
  311.       lastpicked = 0;
  312.     break;
  313.       case HIGHSC:
  314.     if (written)
  315.       {
  316.         curstate = VIEWSCORES;
  317.         glutKeyboardFunc(NULL);
  318.         glutIdleFunc(idlescore);
  319.       }
  320.     break;
  321.       case VIEWSCORES:
  322.     curstate = SELBOARD;
  323.     glutKeyboardFunc(keyboard);
  324.     glutIdleFunc(idle);
  325.     totalpegs = 0;
  326.     for (i=0; i<BOARDSIZE; i++)
  327.       for (j=0; j<BOARDSIZE; j++)
  328.         {
  329.           filled[i][j] = boards[curboard][i][j];
  330.           if (filled[i][j] == FULL)
  331.         totalpegs++;
  332.         }
  333.     break;
  334.       default:
  335.     printf("Unknown state %d, exiting.\n", curstate);
  336.     exit(1);
  337.       }
  338.  
  339.   /* Release a button, reset the array */
  340.   else if (state==GLUT_UP && button==GLUT_LEFT_BUTTON)
  341.     switch (curstate)
  342.       {
  343.       case SELBOARD:
  344.     break;
  345.       case PLAY:
  346.     if (picked <= 0)
  347.       break;
  348.     if ((mid = legalmove()))
  349.       {
  350. #if 0
  351.         //        printf("Erasing (%d,%d).", (mid-1)/BOARDSIZE, (mid-1)%BOARDSIZE);
  352. #endif
  353.         filled[(lastpicked-1)/BOARDSIZE][(lastpicked-1)%BOARDSIZE] = EMPTY;
  354.         filled[(mid-1)/BOARDSIZE][(mid-1)%BOARDSIZE] = EMPTY;
  355.         filled[(picked-1)/BOARDSIZE][(picked-1)%BOARDSIZE] = FULL;
  356.         pegs--;
  357.         /* Check for any legal moves left */
  358.         if (!movesexist())
  359.           {
  360.         /* Display score & "no moves left" */
  361.         printf("No moves remaining.  You finished with %d pegs left.\n",
  362.                pegs);
  363.         playdone = 1;
  364. #if 0
  365.         //        exit(0);
  366. #endif
  367.           }
  368.       }
  369.     else if (lastpicked != 0)
  370.       filled[(lastpicked-1)/BOARDSIZE][(lastpicked-1)%BOARDSIZE] = FULL;
  371.     break;
  372.       case HIGHSC:
  373.     break;
  374.       case VIEWSCORES:
  375.     break;
  376.       default:
  377.     printf("Unknown state %d, exiting.\n", curstate);
  378.     exit(1);
  379.       }
  380.  
  381.   else
  382.     tbMouse(button, state, x, y);
  383.  
  384. #if 0
  385.   else if (state == GLUT_DOWN && button == GLUT_RIGHT_BUTTON)
  386.     tbStartMotion(x, y, button, glutGet(GLUT_ELAPSED_TIME));
  387.   else if (state == GLUT_UP && button == GLUT_RIGHT_BUTTON)
  388.     tbStopMotion(button, glutGet(GLUT_ELAPSED_TIME));
  389. #endif
  390.  
  391.   glutPostRedisplay();
  392.  
  393. }
  394.  
  395. void motion(int x, int y)
  396. {
  397.   tbMotion(x, y);
  398.   if (mouse_button == GLUT_LEFT_BUTTON)
  399.     picked = pick(x,y);
  400.   
  401.   glutPostRedisplay();
  402. }
  403.  
  404.  
  405. void idle(void)
  406. {
  407.   display();
  408. }
  409.   
  410.